<template>
  <div class="order-container">
    <div class="order-items">
      <h2>订单确认</h2>
      <div v-for="item in orderItems" :key="item.id" class="order-item">
        <span>{{ item.name }} x {{ item.count }}</span>
        <span>单价: {{ item.price }}</span>
        <span>小计: {{ item.price * item.count }}</span>
      </div>
      <div class="order-total">
        <span>订单总价: {{ totalOrderPrice }}</span>
      </div>
    </div>
    <div class="address-selector">
      <el-select v-model="selectedAddressId" placeholder="请选择地址">
        <el-option
          v-for="address in addresses"
          :key="address.addressId"
          :label="address.address"
          :value="address.addressId"
        >
        </el-option>
      </el-select>
    </div>
    <div class="confirm-order">
      <el-button type="primary" @click="confirmOrder">确认下单</el-button>
    </div>
  </div>
</template>
<script setup>
import { ref, reactive, computed } from "vue";
import * as cartapi from "@/api/cart";
import * as api from "@/api/address";
import * as orderapi from "@/api/order";
import { useStore } from "vuex";
import { ElMessage } from "element-plus";
import { useRouter } from "vue-router";
const store = useStore();
const getcarts = async () => {
  const { data } = await cartapi.getAllList({
    userId: store.state.userInfo.userId,
  });
  orderItems.value = data;
};
getcarts();
const addresses = ref([]);
const getList = async () => {
  const { data } = await api.getAllList({
    userId: store.state.userInfo.userId,
  });
  addresses.value = data;
};
getList();
const orderItems = ref([]);

const selectedAddressId = ref(
  addresses.value.length > 0 ? addresses.value[0].addressId : ""
); // 默认选择第一个地址

// 计算订单总价
const totalOrderPrice = computed(() =>
  orderItems.value.reduce((total, item) => total + item.price * item.count, 0)
);
const router = useRouter();
async function confirmOrder() {
  // 在这里实现后续的订单确认逻辑，如调用API等
  const { data, message } = await orderapi.addItem({
    userId: store.state.userInfo.userId,
    totalPrice: totalOrderPrice.value,
    status: 1,
    addressId: selectedAddressId.value,
  });
  data ? ElMessage.success(message) : ElMessage.warning(message);
  router.push("/user/myorder");
}
</script>
<style scoped>
.order-container {
  max-width: 800px;
  margin: 20px auto;
  padding: 20px;
  background-color: #fff;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.order-items h2 {
  margin-bottom: 20px;
  color: #333;
  font-size: 24px;
  text-align: center;
}

.order-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 15px;
  margin-bottom: 10px;
  background-color: #f8f8f8;
  border-radius: 8px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.order-item span {
  color: #333;
  font-size: 16px;
}

.order-total {
  margin-top: 20px;
  padding-top: 10px;
  border-top: 1px solid #eee;
  text-align: right;
}

.order-total span {
  color: #333;
  font-weight: bold;
  font-size: 18px;
}

.address-selector {
  margin-top: 30px;
  text-align: center;
}

.address-selector .el-select {
  width: 100%;
  max-width: 400px;
}

.confirm-order {
  margin-top: 20px;
  text-align: center;
}

.confirm-order .el-button {
  padding: 10px 30px;
  font-size: 16px;
}
</style>